package com.platform.service.impl;

import com.platform.dao.MerchantInfoDao;
import com.platform.dao.MerchantInformationDao;
import com.platform.entity.MerchantEntity;
import com.platform.entity.MerchantInfoEntity;
import com.platform.entity.MerchantInfomationEntity;
import com.platform.entity.SysMerchantEntity;
import com.platform.service.MerchantInfoService;
import com.platform.service.MerchantInfomationService;
import com.platform.service.MerchantService;
import com.platform.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.security.krb5.internal.PAData;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商户系统管理员Service实现类
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-04-30 11:22:07
 */
@Service("merchantInfoService")
public class MerchantInfoServiceImpl implements MerchantInfoService {
    @Autowired
    private MerchantInfoDao merchantInfoDao;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private MerchantInformationDao merchantInformationDao;


    @Override
    public MerchantInfoEntity queryObject(Long userId) {
        return merchantInfoDao.queryObject(userId);
    }

    @Override
    public List<MerchantInfoEntity> queryList(Map<String, Object> map) {
        map.put("merchantId",1);
        return merchantInfoDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        map.put("merchantId",1);
        return merchantInfoDao.queryTotal(map);
    }

    @Override
    public int save(MerchantInfoEntity sysMerchant) {
        Long userId = ShiroUtils.getUserId();
        sysMerchant.setCreateUserId(userId);
        //sysMerchant.setMerchantId(1);
        //sysMerchant.setMerchantName("动动体育");
        return merchantInfoDao.save(sysMerchant);
    }

    @Override
    public int update(MerchantInfoEntity sysMerchant) {
        sysMerchant.setMerchantId(1);
        sysMerchant.setMerchantName("动动体育");
        return merchantInfoDao.update(sysMerchant);
    }

    @Override
    public int delete(Long userId) {
        return merchantInfoDao.delete(userId);
    }

    @Override
    public int deleteBatch(Long[] userIds) {
        return merchantInfoDao.deleteBatch(userIds);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveObject(Map<String, Object> param) throws Exception {
        MerchantEntity merchantEntity = new MerchantEntity();
        merchantEntity.setUserId(Long.parseLong(param.get("userid").toString()));
        merchantEntity.setDdPhone(param.get("mobile").toString());
        merchantEntity.setName(param.get("name").toString());
        merchantEntity.setRealname(param.get("realname").toString());
        merchantEntity.setPhone(param.get("phone").toString());
        merchantEntity.setAddress(param.get("address").toString());
        merchantEntity.setIdcard(param.get("idcard").toString());
        merchantEntity.setValue(param.get("value").toString());
        merchantEntity.setValue1(param.get("value1").toString());
        merchantEntity.setLicense(param.get("licence").toString());
        merchantEntity.setOpenLicence(param.get("openLicence").toString());
        merchantEntity.setInstitution(param.get("institution").toString());
        merchantEntity.setValue2(param.get("value2").toString());
        merchantEntity.setValue5(param.get("value5").toString());
        merchantEntity.setValue3(param.get("value3").toString());
        merchantEntity.setValue4(param.get("value4").toString());
        Integer save = merchantService.save(merchantEntity);
        if(save<=0){
            return false;
        }
        MerchantEntity queryMerchant = merchantService.queryObjectByUid(Integer.parseInt(param.get("userid").toString()));
        MerchantInfoEntity sysMerchant = new MerchantInfoEntity();
        sysMerchant.setMerchantId(queryMerchant.getId());
        sysMerchant.setUsername(param.get("username").toString());
        sysMerchant.setEmail(param.get("email").toString());
        sysMerchant.setMobile(param.get("mobile").toString());
        sysMerchant.setStatus(Integer.parseInt(param.get("status").toString()));
        sysMerchant.setPassword("92925488b28ab12584ac8fcaa8a27a0f497b2c62940c8f4fbc8ef19ebc87c43e");//默认密码
        sysMerchant.setUserId(Long.parseLong(param.get("userid").toString()));
        sysMerchant.setCreateUserId(ShiroUtils.getUserId());
        sysMerchant.setMerchantName(queryMerchant.getName());
        sysMerchant.setRemark(param.get("remark").toString());
        sysMerchant.setCreateDate(new Date());

        Integer save2 = merchantInfoDao.save(sysMerchant);
        if(save2<=0){
            throw new Exception();
        }

        MerchantInfomationEntity merchantInfomationEntity = new MerchantInfomationEntity();
        merchantInfomationEntity.setMid(queryMerchant.getId().longValue());
        merchantInfomationEntity.setCreateDate(new Date());
        merchantInfomationEntity.setInstitution(param.get("institution").toString());
        merchantInfomationEntity.setLogo(param.get("logo").toString());
        merchantInfomationEntity.setName(param.get("name").toString());
        merchantInfomationEntity.setStatu(Integer.parseInt(param.get("status").toString()));
        merchantInfomationEntity.setAmount(new BigDecimal(0));
        merchantInfomationEntity.setFrozen(new BigDecimal(0));
        merchantInfomationEntity.setVersion(1);

        Integer save3 = merchantInformationDao.save(merchantInfomationEntity);
        if(save3<=0){
            throw  new Exception();
        }
        return true;
    }
}
